package com.example.study;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @Author : fang.com
 * @CreatTime : 2021-03-30 17:35
 * @Description :
 * @Version :  0.0.1
 */
public class LinkedBlockingQueueTest {
    private int i;
    BlockingQueue<Integer> queue = new LinkedBlockingQueue<Integer>(10);

    class Producer implements Runnable {
        @Override
        public void run() {
            while (true) {
                try {
                    for (int j = 0; j < 10; j++) {
                        System.out.println("Start to produce!" + Thread.currentThread().getName() + "producer capacity is:" + queue.remainingCapacity());
                        queue.put(i++);
                    }
                    System.out.println("producer capacity is:" + queue.remainingCapacity());
                    Thread.sleep(60);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    class Consumer implements Runnable {
        @Override
        public void run() {
            while (true) {
                try {
                    int j = queue.take();
                    System.out.println("Start to take!:" + j + "   " + Thread.currentThread().getName() + "comsumer capacity is:" + queue.remainingCapacity());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] argv) {
        LinkedBlockingQueueTest test = new LinkedBlockingQueueTest();
        new Thread(test.new Producer()).start();
        new Thread(test.new Consumer()).start();
        new Thread(test.new Consumer()).start();
        new Thread(test.new Consumer()).start();
        new Thread(test.new Consumer()).start();
        new Thread(test.new Consumer()).start();
        new Thread(test.new Consumer()).start();
        new Thread(test.new Consumer()).start();
        new Thread(test.new Consumer()).start();
        new Thread(test.new Consumer()).start();
        new Thread(test.new Consumer()).start();
    }
}